import { Navigate, useRoutes } from "react-router-dom";
import { RouteObject } from "@/routers/interface";
import Login from "@/views/login/index";
import lazyLoad from "./utils/lazyLoad";
import React from "react";
import commonRouter from "./modules/common";

// * 处理路由
export const loadRouterArray = (): RouteObject[] => {
	let arr: RouteObject[] = []
	arr.push(...commonRouter())
	return arr;
}

export const rootRouter = (): RouteObject[] => {
	return [
		{
			key: "loginJump",
			title: "登录跳转",
			path: "/",
			skipAuth: true,
			element: <Navigate to="/login" />,
		},
		{
			path: "/login",
			element: <Login />,
			skipAuth: true,
			title: "登录页",
			key: "login",
		},
		...loadRouterArray(),
		{
			path: "/403",
			element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/403"))),
			skipAuth: true,
			title: "403页面",
			key: "403",
		},
		{
			path: "/404",
			element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/404"))),
			skipAuth: true,
			title: "404页面",
			key: "404",
		},
		{
			path: "/500",
			element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/500"))),
			skipAuth: true,
			title: "500页面",
			key: "500",
		},
		{
			key: "other",
			title: "其他路由",
			skipAuth: true,
			path: "*",
			element: <Navigate to="/404" />,
		},
	]
};

const Router = () => {
	const routes = useRoutes(rootRouter());
	return routes;
};

export default Router;
